package com.frcdi.qms.web.controller.masterdata;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.page.TableDataInfo;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.masterdata.domain.Method;
import com.frcdi.qms.masterdata.service.IMethodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 方法Controller
 *
 * @author cell
 * @date 2023-10-16
 */
@Api(tags = "方法")
@RestController
@RequestMapping("/masterData/method")
public class MethodController extends BaseController
{
    @Autowired
    private IMethodService methodService;

    /**
     * 查询方法列表
     */
    @ApiOperation("查询方法列表")
    @PreAuthorize("@ss.hasPermi('masterData:method:list')")
    @GetMapping("/list")
    public TableDataInfo list(Method method)
    {
        startPage();
        List<Method> list = methodService.selectMethodList(method);
        return getDataTable(list);
    }

    /**
     * 导出方法列表
     */
    @ApiOperation("导出方法列表")
    @PreAuthorize("@ss.hasPermi('masterData:method:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Method method)
    {
        List<Method> list = methodService.selectMethodList(method);
        ExcelUtil<Method> util = new ExcelUtil<Method>(Method.class);
        util.exportExcel(response, list, "方法数据");
    }

    /**
     * 获取方法详细信息
     */
    @ApiOperation("获取方法详细信息")
    @PreAuthorize("@ss.hasPermi('masterData:method:query')or @ss.hasPermi('workflow:flowable:list')or @ss.hasPermi('masterData:execution:list')")
    @GetMapping(value = "/{methodId}")
    public AjaxResult getInfo(@PathVariable("methodId") Long methodId)
    {
        return success(methodService.selectMethodByMethodId(methodId));
    }

    /**
     * 新增方法
     */
    @ApiOperation("新增方法")
    @PreAuthorize("@ss.hasPermi('masterData:method:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Method method)
    {
        if (!methodService.checkMethodNameUnique(method)){
            return error("新增方法'"+ method.getMethodName() + "'失败，名称已存在");
        }
        method.setCreateBy(getUsername());

        return success(methodService.insertMethod(method));
    }

    /**
     * 修改方法
     */
    @ApiOperation("修改方法")
    @PreAuthorize("@ss.hasPermi('masterData:method:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Method method)
    {
        if (!methodService.checkMethodNameUnique(method)){
        return error("修改方法'"+ method.getMethodName() + "'失败，名称已存在");
        }
        method.setUpdateBy(getUsername());
        // 编辑时重置所有状态
        method.setStatus(UserConstants.DISABLE);
        method.setAuditResult(UserConstants.AUDIT_NOT_AUDITED);
        method.setSend(UserConstants.SEND_NOT_RETURNED);
        method.setTaskStatus(UserConstants.TASK_NOT);
        return toAjax(methodService.updateMethod(method));
    }

    /**
     * 修改流程编号
     */
    @ApiOperation("修改流程编号")
    @PreAuthorize("@ss.hasPermi('masterData:method:edit')")
    @PutMapping("/method")
    public AjaxResult editById(@RequestBody Method method)
    {
        method.setUpdateBy(getUsername());
        return toAjax(methodService.updateMethod(method));
    }

    /**
     * 删除方法
     */
    @ApiOperation("删除方法")
    @PreAuthorize("@ss.hasPermi('masterData:method:remove')")
	@DeleteMapping("/{methodIds}")
    public AjaxResult remove(@PathVariable Long[] methodIds)
    {
        return toAjax(methodService.deleteMethodByMethodIds(methodIds));
    }

    /**
     * 提交至申请人
     */
    @ApiOperation("提交至申请人")
    @PreAuthorize("@ss.hasPermi('masterData:method:add')or @ss.hasPermi('masterData:method:edit')")
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Method method)
    {
        return toAjax(methodService.submitApplicant(method));
    }

    @ApiOperation("获取编码")
    @PreAuthorize("@ss.hasPermi('masterData:method:add')")
    @GetMapping("/getCode")
    public AjaxResult getcode(){
        return success(methodService.getCode());
    }
}
